#include "signupwidget.h"
#include "ui_signupwidget.h"

#include <QMessageBox>
#include <QJsonObject>
#include <QJsonDocument>
#include <QByteArray>

SignUpWidget::SignUpWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SignUpWidget)
{
    ui->setupUi(this);

    this->setWindowTitle("注册");
    this->setWindowIcon(QIcon(":/img/res/icon.png"));
    this->setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint);

    ui->lineEdit_username->setFocus();

    connect(ui->lineEdit_username, &QLineEdit::returnPressed, [=]() {
        ui->lineEdit_password->setFocus();
    });
    connect(ui->lineEdit_password, &QLineEdit::returnPressed, [=]() {
        ui->lineEdit_confirmPassword->setFocus();
    });
    connect(ui->lineEdit_confirmPassword, SIGNAL(returnPressed()), this, SLOT(on_pushButton_signUp_clicked()));
}

SignUpWidget::~SignUpWidget()
{
    delete ui;
}

void SignUpWidget::slot_finishRequest(QNetworkReply *reply)
{
    if (reply->error()) {
        qDebug() << "SignUp::finishRequest(QNetworkReply *) error";
        QMessageBox::critical(this, tr("失败"), tr("请检查网络连接"), QMessageBox::Ok);
        return;
    }

    QByteArray byteArray = reply->readAll();
    QJsonParseError jsonParseError;
    QJsonDocument parseDocument = QJsonDocument::fromJson(byteArray, &jsonParseError);
    if (jsonParseError.error == QJsonParseError::NoError) {
        if (parseDocument.isObject()) {
            QJsonObject jsonObj = parseDocument.object();
            if (jsonObj.contains("success")) {
                bool isSuccess = jsonObj.value("success").toBool();
                if (isSuccess) {
                    QMessageBox::information(this, tr(""), tr("注册成功"), QMessageBox::Ok);
                    this->close();
                } else {
                    QMessageBox::critical(this, "注册失败", jsonObj.value("message").toString(), QMessageBox::Ok);
                    ui->lineEdit_password->clear();
                    ui->lineEdit_confirmPassword->clear();
                    ui->lineEdit_username->setFocus();
                }
            }
        }
    }

    reply->deleteLater();
}

void SignUpWidget::on_pushButton_signUp_clicked()
{
    QString username = ui->lineEdit_username->text();
    QString password = ui->lineEdit_password->text();

    if(username.isEmpty() || password.isEmpty()) {
        QMessageBox::warning(this, tr("警告"), tr("用户名或密码不能为空"), QMessageBox::Ok);
        return;
    }

    if (password.compare(ui->lineEdit_confirmPassword->text()) != 0) {
        QMessageBox::warning(this, tr("警告"), tr("两次输入的密码不同"), QMessageBox::Ok);
        ui->lineEdit_password->clear();
        ui->lineEdit_confirmPassword->clear();
        ui->lineEdit_password->setFocus();
        return;
    }

    if (!ui->checkBox_agreement->isChecked()) {
        QMessageBox::information(this, tr("注意"), tr("请同意用户条款"), QMessageBox::Ok);
        ui->checkBox_agreement->setFocus();
        return;
    }

    QJsonObject jsonObj;
    jsonObj.insert("username", username);
    jsonObj.insert("password", password);

    QJsonDocument jsonDoc;
    jsonDoc.setObject(jsonObj);

    QByteArray data = jsonDoc.toJson(QJsonDocument::Compact);

    //    m_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
    m_request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
    m_request.setUrl(QUrl(m_signUpUrl));

    m_manager.post(m_request, data);
    connect(&m_manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(slot_finishRequest(QNetworkReply *)));
}

